
from PySide2.QtCore import *
from PySide2.QtGui import *
from PySide2.QtWidgets import *
import os

from ui.Ui_main_window import Ui_MainWindow
from app.window.script_widget import Script_widget
from app.window.execute_widget import Execute_widget
from app.window.planed_task_widget import Planed_task_widget
from app.window.devices_widget import Devices_widget
from app.window.log_widget import Log_widget
from app.window.public_script_manager_widget import Public_script_manager_widget
from app.window.private_script_manager_dialog import Private_script_manager_dialog
from app.window.private_script_editor_dialog import Private_script_editor_dialog
from app.window.device_searcher_widget import Device_searcher_widget
from app.window.device_new_dialog import Device_new_dialog
from worker.tasks_worker import Task_worker
from app.window.custom_ui.Ui_custom import HorizontalTabBar
from app.window.custom_ui.Ui_custom import TabBarStyle
from app.window.new_database_dialog import New_datbase_dialog
import time
import globals.var

class Windows_signal(QObject):
    reload_execute_data = Signal()

class Window(QMainWindow,Ui_MainWindow):

    def __init__(self,):
        super(Window, self).__init__()
        self.signal = Windows_signal()

        self.setup_ui()
        # self.resize(800,600)
        # self.init_signal_slot()

    # 初始化ui
    def setup_ui(self):
        self.setupUi(self)



        self.verticalLayout = QVBoxLayout(self.centralwidget)
        self.verticalLayout.setObjectName(u"verticalLayout")

        # self.dockWidget = QDockWidget()
        # self.dockWidget.setWidget()

        self.tabWidget = QTabWidget()
        self.tabWidget.setObjectName(u"tabWidget")
        self.verticalLayout.addWidget(self.tabWidget)

        self.tabWidget.setTabBar(HorizontalTabBar())
        self.tabWidget.tabBar().setStyle(TabBarStyle())
        self.tabWidget.setTabPosition(QTabWidget.West)
        self.tabbar = self.tabWidget.tabBar()
        self.tabbar.currentChanged.connect(self.on_tab_changed)
        # self.tabbar.tabBarClicked.connect(self.on_tab_changed)

        # 日志
        self.dialog_log = Log_widget()
        # self.verticalLayout.addWidget(self.dialog_log)

        self.widget_devices = Devices_widget()
        self.tabWidget.addTab(self.widget_devices, "设备列表")

        self.widget_public_script_manager = Public_script_manager_widget()
        self.tabWidget.addTab(self.widget_public_script_manager, "公有脚本")

        self.widget_task = Planed_task_widget()
        self.tabWidget.addTab(self.widget_task, "计划任务")

        self.widget_execute = Execute_widget()
        self.tabWidget.addTab(self.widget_execute, "单次执行")
        
        # self.widget_script = Script_widget()
        # self.tabWidget.addTab(self.widget_script, "Script")


        # 脚本管理
        self.dialog_script_manager = Private_script_manager_dialog()
        self.dialog_script_editer = Private_script_editor_dialog()

        # 设备搜索
        self.dialog_device_searcher = Device_searcher_widget()

        # 新建设备
        self.dialog_device_new = Device_new_dialog()


        # 新建数据库
        self.dialog_new_database = New_datbase_dialog()
        self.dialog_new_database.hide()
        # action
        self.action_show_logs.changed.connect(self.action_show_logs_changed)
        self.action_new_database.triggered.connect(self.dialog_new_database.show)
        self.action_reload_database.triggered.connect(self.slot_reload_database)


        # signla slot
        self.signal.reload_execute_data.connect(self.widget_execute.slot_reload_data)


    def init_data(self):
        self.widget_devices.init_data()
        self.widget_public_script_manager.init_data()
        self.widget_task.init_data()

    def slot_reload_database(self):
        # 打开文件选择对话框
        file_path = QFileDialog.getOpenFileName(self,'选择文件','./database',"数据库文件(*.db)")
        if not file_path:
            return
        file_path = file_path[0]
        globals.var.dbname = file_path
        self.init_data()
        print(file_path,type(file_path))
        

    def action_show_logs_changed(self):
        if self.action_show_logs.isChecked():
            pos = self.pos()
            self.dialog_log.move(pos.x(),self.height() + pos.y() + 35)
            self.dialog_log.show()
        else:
            self.dialog_log.hide()

    
    def set_tab(self,tab_name:str):
        for i in range(self.tabWidget.count()):
                if tab_name == self.tabWidget.tabText(i):
                    self.tabWidget.setCurrentIndex(i)


    def on_tab_changed(self,index:int):
        if '单次执行' == self.tabWidget.tabText(index):
            # 重载ComboBox 数据
            self.signal.reload_execute_data.emit()
        # if '计划任务' == self.tabWidget.tabText(index):
        #     self.widget_task.slot_show()

    # def moveEvent(self,event):
    #     new_pos = event.pos()
    #     self.dialog_log.move(new_pos.x(),self.height() + new_pos.y() + 5)
    #     # print('主窗口移动:',new_pos)
    #     super().moveEvent(event)

    # def resizeEvent(self,event):
    #     pos = self.pos()
    #     self.dialog_log.move(pos.x(),self.height() + pos.y() + 35)
    #     # print('resizeEvent',height)
    #     super().resizeEvent(event)
    
    def closeEvent(self, event: QCloseEvent) -> None:
        self.dialog_log.close()
        # 关闭任务检测线程
        self.widget_task.worker.stop_work()
        self.widget_task.workerThread.quit()
        self.widget_task.workerThread.wait()
        return super().closeEvent(event)
    
    # def hideEvent(self, event: QHideEvent) -> None:
    #     self.dialog_log.hide()
    #     return super().hideEvent(event)
    
    # def showEvent(self, event: QShowEvent) -> None:
    #     pos = self.pos()
    #     self.dialog_log.move(pos.x(),self.height() + pos.y() + 35)
    #     self.dialog_log.show()
    #     return super().showEvent(event)


